package com.newshub.admin.controller;

import com.newshub.common.Result;
import com.newshub.admin.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 仪表盘数据控制器
 * 提供数据可视化相关的统计接口
 */
@RestController
@RequestMapping("/api/dashboard")
@CrossOrigin(origins = "*")
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    /**
     * 获取仪表盘概览数据
     * 包含所有图表需要的数据
     */
    @GetMapping("/overview")
    public Result<Map<String, Object>> getDashboardOverview() {
        try {
            Map<String, Object> overview = dashboardService.getDashboardOverview();
            return Result.success(overview);
        } catch (Exception e) {
            return Result.error("获取仪表盘数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取新闻趋势数据
     * @param period 统计周期：day-按天，week-按周，month-按月
     */
    @GetMapping("/news-trend")
    public Result<Map<String, Object>> getNewsTrend(@RequestParam(defaultValue = "day") String period) {
        try {
            Map<String, Object> trendData = dashboardService.getNewsTrend(period);
            return Result.success(trendData);
        } catch (Exception e) {
            return Result.error("获取新闻趋势数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取信源分布数据
     */
    @GetMapping("/source-distribution")
    public Result<Map<String, Object>> getSourceDistribution() {
        try {
            Map<String, Object> distributionData = dashboardService.getSourceDistribution();
            return Result.success(distributionData);
        } catch (Exception e) {
            return Result.error("获取信源分布数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取分类统计数据
     */
    @GetMapping("/category-stats")
    public Result<Map<String, Object>> getCategoryStats() {
        try {
            Map<String, Object> categoryData = dashboardService.getCategoryStats();
            return Result.success(categoryData);
        } catch (Exception e) {
            return Result.error("获取分类统计数据失败: " + e.getMessage());
        }
    }
}

